package edu.stanford.bmir.protege.web.client.ui.ontology.stkos.merger.currentdesc;

import java.util.Collection;

import com.google.common.base.Optional;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.Widget;
import com.gwtext.client.widgets.tree.TreeNode;

import edu.stanford.bmir.protege.web.client.project.Project;
import edu.stanford.bmir.protege.web.client.rpc.data.EntityData;
import edu.stanford.bmir.protege.web.client.ui.editor.EditorContextMapper;
import edu.stanford.bmir.protege.web.client.ui.editor.EditorCtx;
import edu.stanford.bmir.protege.web.client.ui.editor.EditorPresenter;
import edu.stanford.bmir.protege.web.client.ui.editor.EntityDataContextSelector;
import edu.stanford.bmir.protege.web.client.ui.editor.OWLEntityDataContext;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.common.StkosPanelType;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.merger.common.StkosMergerIndividualTreePanel;
import edu.stanford.bmir.protege.web.client.ui.portlet.AbstractOWLEntityPortlet;
import edu.stanford.bmir.protege.web.shared.entity.OWLEntityData;

public class StkosMergerCurrentDescriptionPortlet extends
		AbstractOWLEntityPortlet {

	private TreeNode selectedTreeNode;

	private ScrollPanel sp;

	private EditorPresenter editorPresenter;

	private StkosMergerIndividualTreePanel individualTreePanel;

	private StkosPanelType type;

	private Project accordingProject;

	public StkosMergerCurrentDescriptionPortlet(Project project) {
		super(project);
	}

	@Override
	public Collection<EntityData> getSelection() {
		return null;
	}

	@Override
	public void onRefresh() {
		if (isIndividualTreePanelActivity()) {
			individualTreePanel.onRefresh();
		}
		if (isEditorPresenterActivity()) {
			editorPresenter.setEditorContext(getEditorContext());
		}
	}

	@Override
	public void reload() {
	}

	@Override
	public void initialize() {
		setHeight(210);

		showPanel(StkosPanelType.CLASS_DESCRIPTION);
	}

	public void setAccordingProject(Project accordingProject) {
		this.accordingProject = accordingProject;
		if (individualTreePanel != null) {
			individualTreePanel.setAccordingProject(accordingProject);
		}
	}

	protected boolean isEditorPresenterActivity() {
		boolean f = false;
		if (editorPresenter != null
				&& StkosPanelType.CLASS_DESCRIPTION.equals(type)) {
			f = true;
		}
		return f;
	}

	protected boolean isIndividualTreePanelActivity() {
		boolean f = false;
		if (individualTreePanel != null
				&& StkosPanelType.INDIVIDUAL_TREE.equals(type)) {
			f = true;
		}
		return f;
	}

	protected void removeAllItems() {
		if (sp != null) {
			sp.addStyleName("x-hide-display");
		}
		if (individualTreePanel != null) {
			individualTreePanel.hide();
		}
	}

	public void showPanel(StkosPanelType type) {
		this.type = type;
		removeAll();
		removeAllItems();
		if (StkosPanelType.CLASS_DESCRIPTION.equals(type)) {
			setTitle("当前本体类描述");
			if (editorPresenter == null) {
				addEditorPresenter();
			} else {
				sp.removeStyleName("x-hide-display");
			}
		} else if (StkosPanelType.INDIVIDUAL_TREE.equals(type)) {
			setTitle("当前类个体树");
			if (individualTreePanel == null) {
				addIndividualTreePanel();
			} else {
				individualTreePanel.show();
			}
		} else {
			setTitle("当前本体描述");
		}
	}

	public void setSelectedTreeNode(TreeNode selectedTreeNode) {
		this.selectedTreeNode = selectedTreeNode;
		EntityData entityData = getSelectedEntityData2();
		if (StkosPanelType.CLASS_DESCRIPTION.equals(type)) {
			if (editorPresenter == null) {
				addEditorPresenter();
			} else {
				editorPresenter.setEditorContext(getEditorContext());
			}
		} else if (StkosPanelType.INDIVIDUAL_TREE.equals(type)) {
			if (individualTreePanel == null) {
				addIndividualTreePanel();
			} else {
				individualTreePanel.setOwnerCls(entityData);
			}
		}
		doLayout();
	}

	protected void addEditorPresenter() {
		EditorContextMapper contextMapper = new EditorContextMapper();
		contextMapper.registerSelector(new EntityDataContextSelector());
		editorPresenter = new EditorPresenter(getProjectId(), contextMapper);
		if (editorPresenter == null) {
			return;
		}
		editorPresenter.setEditorContext(getEditorContext());
		Widget editorHolder = editorPresenter.getEditorHolder();
		sp = new ScrollPanel(editorHolder);
		sp.setSize("100%", "100%");
		add(sp);
		doLayout();
	}

	protected void addIndividualTreePanel() {
		EntityData entityData = getSelectedEntityData2();
		individualTreePanel = new StkosMergerIndividualTreePanel(getProject(),
				this.accordingProject, entityData);
		add(individualTreePanel);
		doLayout();
	}

	protected Optional<EditorCtx> getEditorContext() {
		if (selectedTreeNode == null) {
			return Optional.absent();
		}
		EntityData entityData = (EntityData) selectedTreeNode.getUserObject();
		if (entityData == null) {
			return Optional.absent();
		}
		Optional<OWLEntityData> sel = toOWLEntityData(entityData);
		if (!sel.isPresent()) {
			return Optional.absent();
		}
		if (getProject() == null || getProject().getProjectId() == null) {
			return Optional.absent();
		}
		return Optional.<EditorCtx> of(new OWLEntityDataContext(getProject()
				.getProjectId(), sel.get()));
	}

	protected EntityData getSelectedEntityData2() {
		EntityData entityData = null;
		if (selectedTreeNode != null
				&& selectedTreeNode.getUserObject() != null) {
			entityData = (EntityData) selectedTreeNode.getUserObject();
		}
		return entityData;
	}
}
